package com.sunmouse.web;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.swing.text.html.parser.Entity;

import com.sunmouse.util.FileUtil;


public class HttpUrlConn {
	static String path = "http://imgsrc.baidu.com/forum/w%3D580/sign=01c8cfd46c81800a6ee58906813433d6/5e3cb316fdfaaf51234eb7e48c5494eef11f7ae7.jpg";
	static String des = "D://pic";

	public static void main(String[] args) {
		try {
			URL url = new URL(path);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			int status = conn.getResponseCode();
			if (status == HttpURLConnection.HTTP_OK) {
				InputStream is = conn.getInputStream();
				BufferedOutputStream bos = new BufferedOutputStream(
						new FileOutputStream(FileUtil.newImageFile(des)));
//				FileOutputStream fos = new FileOutputStream(
//						FileUtil.newImageFile(des));
				byte[] b = new byte[128];
				int len = 0;
				while ((len = is.read(b)) != -1) {
					bos.write(b, 0, len);
					bos.flush();
				}
				System.out.println("download successfully");
				bos.close();
				conn.disconnect();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
